﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IP.Core.IPData;
using IP.Core.IPUserService;
using WebDS.CDBNames;
using WebDS;
using IP.Core.IPCommon;
using WebUS;

public partial class Quantri_F830_PhanQuyenTaiLieu : System.Web.UI.Page
{
    #region Members
    
    #endregion

    #region Data Structures

    #endregion

    #region Private Methods
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!this.IsPostBack)
            {

                load_cbo_nhan_vien();
                load_cbo_tai_lieu();
                load_cbo_tai_lieu_user();
            }

        }
        catch (Exception v_e)
        {
            this.Response.Write(v_e.ToString());
        }

    }
    public string get_mapping_ten_tai_lieu(decimal i_dc_id)
    {
        try
        {
            string v_str_tai_lieu = "";
            US_CM_DM_TU_DIEN v_us = new US_CM_DM_TU_DIEN(i_dc_id);
            v_str_tai_lieu = v_us.strTEN;
            return v_str_tai_lieu;
        }
        catch (Exception v_e)
        {
            throw v_e;
        }

    }

    private void load_cbo_nhan_vien()
    {
        try
        {
            US_V_DM_NHAN_VIEN v_us_nv = new US_V_DM_NHAN_VIEN();
            DS_V_DM_NHAN_VIEN v_ds_nv = new DS_V_DM_NHAN_VIEN();
            v_us_nv.FillDataset(v_ds_nv);
            m_cbo_user.DataSource = v_ds_nv.V_DM_NHAN_VIEN;
            m_cbo_user.DataTextField = V_DM_NHAN_VIEN.HO_VA_TEN;
            m_cbo_user.DataValueField = V_DM_NHAN_VIEN.ID;
            m_cbo_user.DataBind();
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    private void load_cbo_tai_lieu()
    {
        try
        {
            US_CM_DM_TU_DIEN v_us_tai_lieu = new US_CM_DM_TU_DIEN();
            DS_CM_DM_TU_DIEN v_ds_tai_lieu = new DS_CM_DM_TU_DIEN();
            v_us_tai_lieu.FillDataset(v_ds_tai_lieu, " WHERE ID_LOAI_TU_DIEN = "+ (int)e_loai_tu_dien.TAI_LIEU +" AND ID NOT IN (SELECT ID_TAI_LIEU FROM DM_PHAN_QUYEN_TAI_LIEU WHERE ID_LOAI_TU_DIEN = " + (int)e_loai_tu_dien.TAI_LIEU + " AND ID_USE =" + CIPConvert.ToDecimal(m_cbo_user.SelectedValue) + ")");
            m_lst_tai_lieu.DataSource = v_ds_tai_lieu.CM_DM_TU_DIEN;
            m_lst_tai_lieu.DataTextField = CM_DM_TU_DIEN.TEN;
            m_lst_tai_lieu.DataValueField = CM_DM_TU_DIEN.ID;
            m_lst_tai_lieu.DataBind();
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    private void load_cbo_tai_lieu_user()
    {
        try
        {
            US_CM_DM_TU_DIEN v_us_tai_lieu = new US_CM_DM_TU_DIEN();
            DS_CM_DM_TU_DIEN v_ds_tai_lieu = new DS_CM_DM_TU_DIEN();
            v_us_tai_lieu.FillDataset(v_ds_tai_lieu, " WHERE ID IN (SELECT ID_TAI_LIEU FROM DM_PHAN_QUYEN_TAI_LIEU WHERE ID_LOAI_TU_DIEN = " + (int)e_loai_tu_dien.TAI_LIEU + " AND ID_USE =" + CIPConvert.ToDecimal(m_cbo_user.SelectedValue) + ")");
            m_lst_tai_lieu_user.DataSource = v_ds_tai_lieu.CM_DM_TU_DIEN;
            m_lst_tai_lieu_user.DataTextField = CM_DM_TU_DIEN.TEN;
            m_lst_tai_lieu_user.DataValueField = CM_DM_TU_DIEN.ID;
            m_lst_tai_lieu_user.DataBind(); ;
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    private void update_quyen_chuc_nang()
    {
        try
        {
            m_lbl_mess.Text = "";
            string v_str_id_chuc_nangs = "";
            foreach (ListItem ltTemp in this.m_lst_tai_lieu_user.Items)
            {

                v_str_id_chuc_nangs += ltTemp.Value + ",";
            }
            US_DM_PHAN_QUYEN_TAI_LIEU v_us_quyen_tai_lieu = new US_DM_PHAN_QUYEN_TAI_LIEU();
            v_us_quyen_tai_lieu.update_quyen_tai_lieu(CIPConvert.ToDecimal(m_cbo_user.SelectedValue), v_str_id_chuc_nangs);
            m_lbl_mess.Text = "Cập nhật quyền sử dụng chức năng cho nhóm thành công";
        }
        catch (Exception v_e)
        {
            m_lbl_mess.Text = "Lỗi trong quá trình cập nhật bản ghi.";
            throw v_e;
        }
    }

    #endregion

    //
    //
    // events
    //
    //


    protected void m_cmd_right_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            while (m_lst_tai_lieu.Items.Count > 0 && m_lst_tai_lieu.SelectedItem != null)
            {
                ListItem selectedItem = m_lst_tai_lieu.SelectedItem;
                selectedItem.Selected = false;
                m_lst_tai_lieu_user.Items.Add(selectedItem);
                m_lst_tai_lieu.Items.Remove(selectedItem);
            }

        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }

    }

    protected void m_cmd_right_all_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            foreach (ListItem ltTemp in this.m_lst_tai_lieu.Items)
            {
                this.m_lst_tai_lieu_user.Items.Add(ltTemp);
            }
            this.m_lst_tai_lieu.Items.Clear();


        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }

    }
    protected void m_cmd_left_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            while (m_lst_tai_lieu_user.Items.Count > 0 && m_lst_tai_lieu_user.SelectedItem != null)
            {
                ListItem selectedItem = m_lst_tai_lieu_user.SelectedItem;
                selectedItem.Selected = false;
                m_lst_tai_lieu.Items.Add(selectedItem);
                m_lst_tai_lieu_user.Items.Remove(selectedItem);
            }

        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }

    }
    protected void m_cmd_left_all_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            foreach (ListItem ltTemp in this.m_lst_tai_lieu_user.Items)
            {
                this.m_lst_tai_lieu.Items.Add(ltTemp);
            }
            this.m_lst_tai_lieu_user.Items.Clear();


        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cmd_cap_nhat_Click(object sender, EventArgs e)
    {
        try
        {
            m_lbl_mess.Text = "";
            update_quyen_chuc_nang();

        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cbo_user_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            load_cbo_tai_lieu();
            load_cbo_tai_lieu_user();

        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
}